package com.kurt.controller;

import com.kurt.annotation.Log;
import com.kurt.common.PageResult;
import com.kurt.common.Result;
import com.kurt.dto.SysDictTypeDTO;
import com.kurt.dto.create.SysDictTypeCreateRequest;
import com.kurt.dto.query.SysDictTypeQueryRequest;
import com.kurt.dto.update.SysDictTypeUpdateRequest;
import com.kurt.entity.SysDictType;
import com.kurt.service.SysDictTypeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典类型控制器
 * 处理数据字典类型相关的HTTP请求
 */
@Slf4j
@RestController
@RequestMapping("/dict-types")
@RequiredArgsConstructor
public class SysDictTypeController extends BaseController {

    private final SysDictTypeService dictTypeService;

    /**
     * 创建数据字典类型
     *
     * @param createRequest 创建请求
     * @return 字典类型信息
     */
    @PostMapping
    @Log(title = "创建数据字典类型", businessType = 1)
    public Result<SysDictTypeDTO> createDictType(@RequestBody SysDictTypeCreateRequest createRequest) {
        try {
            createRequest.setCreateUserId(getCurrentUserId());
            log.info("创建数据字典类型请求: {}", createRequest.getDictType());
            SysDictTypeDTO dictType = dictTypeService.createDictType(createRequest);
            return Result.success("创建成功", dictType);
        } catch (Exception e) {
            log.error("创建数据字典类型失败: {}", e.getMessage(), e);
            return Result.error("创建失败: " + e.getMessage());
        }
    }

    /**
     * 更新数据字典类型
     *
     * @param updateRequest 更新请求
     * @return 字典类型信息
     */
    @PutMapping
    @Log(title = "更新数据字典类型", businessType = 2)
    public Result<SysDictTypeDTO> updateDictType(@RequestBody SysDictTypeUpdateRequest updateRequest) {
        try {
            updateRequest.setUpdateUserId(getCurrentUserId());
            log.info("更新数据字典类型请求: {}", updateRequest.getId());
            SysDictTypeDTO dictType = dictTypeService.updateDictType(updateRequest);
            return Result.success("更新成功", dictType);
        } catch (Exception e) {
            log.error("更新数据字典类型失败: {}", e.getMessage(), e);
            return Result.error("更新失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取字典类型详情
     *
     * @param id 字典类型ID
     * @return 字典类型信息
     */
    @GetMapping("/{id}")
    public Result<SysDictType> getDictTypeById(@PathVariable("id") Long id) {
        try {
            log.info("获取字典类型详情请求: {}", id);
            SysDictType dictType = dictTypeService.getById(id);
            return Result.success("获取成功", dictType);
        } catch (Exception e) {
            log.error("获取字典类型详情失败: {}", e.getMessage(), e);
            return Result.error("获取失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询数据字典类型
     *
     * @param queryRequest 查询请求
     * @return 分页结果
     */
    @PostMapping("/page")
    public Result<PageResult<SysDictTypeDTO>> getDictTypePage(@RequestBody SysDictTypeQueryRequest queryRequest) {
        try {
            queryRequest.setLoginUserId(getCurrentUserId());
            log.info("分页查询数据字典类型请求: {}", queryRequest);
            Page<SysDictTypeDTO> dictTypePage = dictTypeService.getDictTypePage(queryRequest);

            // 转换为PageResult
            PageResult<SysDictTypeDTO> pageResult = PageResult.of(
                    queryRequest.getPageNum(),
                    queryRequest.getPageSize(),
                    dictTypePage.getTotalElements(),
                    dictTypePage.getContent()
            );

            return Result.success("分页查询成功", pageResult);
        } catch (Exception e) {
            log.error("分页查询数据字典类型失败: {}", e.getMessage(), e);
            return Result.error("分页查询失败: " + e.getMessage());
        }
    }

    /**
     * 查询数据字典类型列表
     *
     * @param queryRequest 查询请求
     * @return 字典类型列表
     */
    @PostMapping("/list")
    public Result<List<SysDictTypeDTO>> getDictTypeList(@RequestBody SysDictTypeQueryRequest queryRequest) {
        try {
            queryRequest.setLoginUserId(getCurrentUserId());
            log.info("查询数据字典类型列表请求: {}", queryRequest);
            List<SysDictTypeDTO> dictTypes = dictTypeService.getDictTypeList(queryRequest);
            return Result.success("查询成功", dictTypes);
        } catch (Exception e) {
            log.error("查询数据字典类型列表失败: {}", e.getMessage(), e);
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 根据字典类型获取字典类型对象
     *
     * @param dictType 字典类型
     * @return 字典类型对象
     */
    @GetMapping("/type/{dictType}")
    public Result<SysDictTypeDTO> getDictTypeByType(@PathVariable("dictType") String dictType) {
        try {
            log.info("根据字典类型获取字典类型对象请求: {}", dictType);
            SysDictTypeDTO dictTypeDTO = dictTypeService.getDictTypeByType(dictType);
            return Result.success("获取成功", dictTypeDTO);
        } catch (Exception e) {
            log.error("根据字典类型获取字典类型对象失败: {}", e.getMessage(), e);
            return Result.error("获取失败: " + e.getMessage());
        }
    }

    /**
     * 删除数据字典类型
     *
     * @param id 字典类型ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Log(title = "数据字典类型管理", businessType = 3, operatorType = 1)
    public Result<Boolean> deleteDictType(@PathVariable("id") Long id) {
        try {
            log.info("删除数据字典类型请求: {}", id);
            boolean success = dictTypeService.deleteById(id);
            return Result.success("删除成功", success);
        } catch (Exception e) {
            log.error("删除数据字典类型失败: {}", e.getMessage(), e);
            return Result.error("删除失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除数据字典类型
     *
     * @param ids 字典类型ID列表
     * @return 删除结果
     */
    @DeleteMapping("/batch")
    @Log(title = "数据字典类型管理", businessType = 3, operatorType = 1)
    public Result<Boolean> deleteDictTypes(@RequestBody List<Long> ids) {
        try {
            log.info("批量删除数据字典类型请求: {}", ids);
            boolean success = dictTypeService.deleteDictTypes(ids);
            return Result.success("批量删除成功", success);
        } catch (Exception e) {
            log.error("批量删除数据字典类型失败: {}", e.getMessage(), e);
            return Result.error("批量删除失败: " + e.getMessage());
        }
    }

    /**
     * 检查字典类型是否存在
     *
     * @param dictType 字典类型
     * @return 是否存在
     */
    @GetMapping("/exists/{dictType}")
    public Result<Boolean> existsByDictType(@PathVariable("dictType") String dictType) {
        try {
            log.info("检查字典类型是否存在请求: {}", dictType);
            boolean exists = dictTypeService.existsByDictType(dictType);
            return Result.success("检查成功", exists);
        } catch (Exception e) {
            log.error("检查字典类型是否存在失败: {}", e.getMessage(), e);
            return Result.error("检查失败: " + e.getMessage());
        }
    }
}
